package com.gm.baseplatform.base.bootstrap;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gm.baseplatform.base.ResourceCachePool;
import com.gm.baseplatform.base.cfg.IConfiguration;
import com.gm.baseplatform.base.module.Module;
import com.gm.baseplatform.base.module.ModuleManager;
import com.gm.baseplatform.core.utils.ApplicationContextUtil;

public class ResourceCacheBoot implements Boot.BootFlag {

	private Log log = LogFactory.getLog(ResourceCacheBoot.class); 
	
	public void boot(Boot boot) throws BootException {
		
		log.info("ResourceCacheBoot...");
		ServletContext context = boot.getServletContext();
		
		IConfiguration configuration = 
			 (IConfiguration)ApplicationContextUtil.getBean("configuration");
		ModuleManager moduleManager = 
			(ModuleManager)ApplicationContextUtil.getBean("moduleManager");
		
		ResourceCachePool resourceCachePool = new ResourceCachePool();
		
		log.info("准备配置缓存...");
		List<?> configs = configuration.loadConfigs();
		resourceCachePool.cacheConfig(configs);
		
		log.info("准备模块缓存...");
		Map<Module,List<?>> moduleTree = moduleManager.buildModuleTreeForPortal();
		resourceCachePool.cacheModuleTree(moduleTree);
		
		context.setAttribute("resourceCachePool", resourceCachePool);
		
	}

	public String getBootRoot() {
		return Boot.BOOT_ROOT_SERVLET;
	}

}
